package com.sky.controller.admin;

import com.sky.Entity.Dish;
import com.sky.dto.DishDTO;
import com.sky.dto.DishQueryPageDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVo;
import com.sky.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理相关操作")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品接口")
    public Result<String> insert(@RequestBody DishDTO dishDTO){
        dishService.insert(dishDTO);
        Long categoryId = dishDTO.getCategoryId();
        String key="dish_"+categoryId;
        redisTemplate.delete(key);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageVo<Dish>> page(DishQueryPageDTO dishQueryPageDTO){
        PageVo<Dish> result=dishService.queryPage(dishQueryPageDTO);
        return Result.success(result);
    }
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result<String> delete(@RequestParam(required = true) List<Long> ids){
        System.out.println("批量删除："+ids);
        dishService.delete(ids);

        //删除全部
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVo> findById(@PathVariable Long id){
        DishVo dishVo=dishService.queryById(id);
        return Result.success(dishVo);
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);

        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("起售停售菜品")
    public Result<String> setStatus(@PathVariable String status,String id){
        DishDTO dishDTO=new DishDTO();
        dishDTO.setId(Long.valueOf(id));
        dishDTO.setStatus(Integer.valueOf(status));
        dishService.update(dishDTO);

        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查菜品")
    public Result<List<Dish>> getDishsById(Long categoryId){
        List<Dish> list=dishService.getDishsById(categoryId);
        return Result.success(list);
    }


}






































